package be.kuleuven.thesis.widget;

import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.RelativeSizeSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import be.kuleuven.thesis.R;
import be.kuleuven.thesis.framework.Song;
import be.kuleuven.thesis.pages.Search;
import be.kuleuven.thesis.status.Player;

public class SearchSongAdapter extends ArrayAdapter<Song>{
	private static final String TAG = "SongAdpt";
	private static final int textViewResourceId = R.layout.rij_search_song;
	private Resources res;
	//private DrawableManager dm = new DrawableManager();
	
	public SearchSongAdapter(Search context, List<Song> objects, Resources res) {
		super(context, textViewResourceId, objects);
		this.res = res;
	}
	
	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (view == null ){//|| this.getItemViewType(position) != textViewResourceId){
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(textViewResourceId, null);
			Log.d(TAG, "inflated songs view");
			
			holder = new ViewHolder();
			holder.title = (TextView) view.findViewById(R.id.search_title);
            holder.artist = (TextView) view.findViewById(R.id.search_artist);
            holder.album = (TextView) view.findViewById(R.id.search_album);
            holder.icon = (ImageView) view.findViewById(R.id.icon);
            holder.addToCurrent = (Button) view.findViewById(R.id.add_to_current);
            holder.songInfo = (LinearLayout) view.findViewById(R.id.song_info);

            view.setTag(holder);
		}
		else {
			holder = (ViewHolder) view.getTag();
		}
	        
        Song song = this.getItem(position);
        if (song != null) {
        	
            Spannable str = new SpannableString(song.getTitle()+" ("+song.getYear()+")");
            str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, str.length() - 7, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            str.setSpan(new RelativeSizeSpan(0.8f), str.length()-7, str.length()-1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            holder.title.setText(str);
            
            if (holder.artist == null)
            	Log.e(TAG, "textview author not found");
            holder.artist.setText(song.getArtist());
            
            if (holder.album == null)
            	Log.e(TAG, "textview album not found");
            holder.album.setText(song.getAlbum());
            
            if(holder.icon != null && song.getImage() != -1) {
            	holder.icon.setImageDrawable(res.getDrawable(song.getImage()));
            	//dm.fetchDrawableOnThread(u.getImage(), icon);
            }
            
            holder.songInfo.setFocusable(true);
            holder.songInfo.setClickable(true);
            holder.songInfo.setTag(song);
            holder.songInfo.setOnClickListener(new View.OnClickListener() {
            	@Override
            	public void onClick(View view) {
            		Song song = (Song) view.getTag();
            		Player.getInstance().playSong(song);
            		((Search) getContext()).switchTab(1);
            	}
            });
            
            holder.addToCurrent.setTag(song);
            holder.addToCurrent.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View view) {
					Song song = (Song) view.getTag();
					Player.getInstance().addSongToCurrentPlaylist(song);
				}
			});
        } else
        	Log.e(TAG, "Song on position " + position + "not found!");
        return view;
	}
	
	static class ViewHolder {
		LinearLayout songInfo;
		TextView title;
        TextView artist;
        TextView album;
        ImageView icon;
        Button addToCurrent;
    }
}
